package com.health.healthSys.service.impl;

import com.health.healthSys.domain.HealthInfoLike;
import com.health.healthSys.mapper.HealthInfoLikeMapper;
import com.health.healthSys.service.IHealthInfoLikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 资讯点赞Service业务层处理
 */
@Service
public class HealthInfoLikeServiceImpl implements IHealthInfoLikeService {
    @Autowired
    private HealthInfoLikeMapper healthInfoLikeMapper;

    @Override
    @Transactional
    public boolean likeInfo(Long infoId, Long userId) {
        // 检查是否已经点赞
        if (checkLikeStatus(infoId, userId)) {
            return false;
        }
        
        HealthInfoLike like = new HealthInfoLike();
        like.setInfoId(infoId);
        like.setUserId(userId);
        return healthInfoLikeMapper.insert(like) > 0;
    }

    @Override
    @Transactional
    public boolean unlikeInfo(Long infoId, Long userId) {
        return healthInfoLikeMapper.deleteByInfoIdAndUserId(infoId, userId) > 0;
    }

    @Override
    public boolean checkLikeStatus(Long infoId, Long userId) {
        return healthInfoLikeMapper.selectByInfoIdAndUserId(infoId, userId) != null;
    }

    @Override
    public int getLikeCount(Long infoId) {
        return healthInfoLikeMapper.selectCountByInfoId(infoId);
    }
} 